package cn.dodgeball.java;

import cn.dodgeball.util.Const;

import javax.swing.*;
import java.awt.*;


public class Timer extends JPanel implements Runnable {
    private boolean isAlive = true;

    private JLabel timer = new JLabel("生存时间：" + 0);

    public Timer() {
        init();
    }

    private void init() {
        setBounds(0, 0, 400, 60);
        setOpaque(false);
        setLayout(null);

        // 文字label
        timer.setFont(Const.SETFONT);
        timer.setForeground(Color.yellow);
        timer.setBounds(0, 0, 600, 60);
        timer.setHorizontalAlignment(SwingConstants.LEFT);
        add(timer);
    }

    @Override
    public void run() {
        while (isAlive && Const.isGaming) {
            Const.currentTime = System.currentTimeMillis();
            long time = Const.currentTime - Const.startTime;
            if (Const.isGold && time > Const.goldTime)// 无敌三秒
                Const.isGold = false;
            timer.setText("生存时间：" + time / 1000 + "." + time % 1000 + "S");
        }

    }

    @Override
    public void removeNotify() {
        super.removeNotify();
        isAlive = false;
    }

}
